package com.zhdl.common.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

@Configuration
public class TaskSchedulerConfig {

    public static ConcurrentHashMap<String, ScheduledFuture<?>> cache = new ConcurrentHashMap<String, ScheduledFuture<?>>();

    @Configuration
    public class ScheduleConfig {
        //         用来存入线程执行情况, 方便于停止定时任务时使用
//        public static ConcurrentHashMap<String, ThreadPoolTaskScheduler> cache= new ConcurrentHashMap<String, ThreadPoolTaskScheduler>();
        @Bean
        public ThreadPoolTaskScheduler taskScheduler1() {
            ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
            threadPoolTaskScheduler.setPoolSize(50);                        // 线程池大小
            threadPoolTaskScheduler.setThreadNamePrefix("taskScheduler-");   // 线程名称
            threadPoolTaskScheduler.setAwaitTerminationSeconds(60);         // 等待时长
            threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);  // 调度器shutdown被调用时等待当前被调度的任务完成
            return threadPoolTaskScheduler;
        }
    }

}
